<?php

namespace App\Models;

use Auth;
use DB;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Qh_LitQuestion extends BaseModel
{
    protected $table = 'qh_lit_questions';

    protected $guarded = [];

    protected $appends = ['userResult', 'userAnswer', 'questionTypeName', 'userQuestionRate'];

    /**
     * 用户的答题结果
     * @return bool|mixed|null
     */
    public function getUserResultAttribute()
    {
        $result = null;
        if (Auth::guard('api')->check()) {
            $user = Auth::guard('api')->user();
            $qId = intval('999999'.$this->id);
            $result = DB::table('qh_user_question_logs')->where('userId', $user->id)->where('questionId', $qId)->value('result');
        }

        return $result;
    }

    /**
     * 用户的答案
     * @return mixed|null
     */
    public function getUserAnswerAttribute() {
        $answer = null;
        if (Auth::guard('api')->check()) {
            $user = Auth::guard('api')->user();
            $qId = intval('999999'.$this->id);
            $answer = DB::table('qh_user_question_logs')->where('userId', $user->id)->where('questionId', $qId)->value('userAnswer');
        }

        return $answer;
    }

    /**
     * 用户答题正确率
     */
    public function getUserQuestionRateAttribute(): float
    {
        $rate = 0;
        $sum = $this->rightCount+$this->wrongCount;
        if ($sum > 0) {
            $rate = round(($this->wrongCount / $sum) * 100, 2);
        }
        return $rate;
    }

    /**
     * options
     * @param $v
     */
    public function getOptionsAttribute($v)
    {
        if (request()->is('admin/*')) return $v;

        if (empty($v)) return [];

        $options = explode('|NTK|', $v);
        $optionList = [];
        foreach ($options as $o) {
            if(empty($o)) continue;
            $_o = explode('、',$o, 2);
            if (empty($_o) || count($_o) < 2) {
                $_o_e = mb_substr($o, 1, 1, 'UTF-8');
                if ($_o_e == '.') {
                    $_o = explode('.',$o, 2);
                }else {
                    $compare_str = mb_convert_kana($_o_e, 'R', 'UTF-8'); // 将半角句号转换为全角句号
                    if ($_o_e == $compare_str) {
                        $_o = explode('．',$o, 2);
                    };
                }
            }

            if(empty($_o) || count($_o) < 2) continue;
            $optionList[] = [
                'key' => trim(strip_tags($_o[0])),
                'value' => replaceStaticFileUrlInString($_o[1]),
            ];
        }

        return $optionList;
    }

    /**
     * 获取题型名称
     * @return mixed|null
     */
    public function getQuestionTypeNameAttribute() {
        return Qh_QuestionType::where('id', $this->questionTypeId)->value('name');
    }

    public function getTitleAttribute($v) {
        return replaceStaticFileUrlInString($v);
    }

    public function getAnswerAttribute($v) {
        if ($this->questionTypeId == 100 ||
            $this->questionTypeId == 1 ||
            $this->questionTypeId == 2 ||
            $this->questionTypeId == 3 ||
            $this->questionTypeId == 7) return trim(strip_tags($v));
        return replaceStaticFileUrlInString($v);
    }

    public function getAnalysisAttribute($v) {
        return replaceStaticFileUrlInString($v);
    }

    public function parent(): BelongsTo
    {
        return $this->belongsTo(Qh_Question::class, 'parentId');
    }
}
